$(document).ready(function() {
	// 点击提交注册
	$("#regist").click(function() {
		var email = $("#email").val();
		var password = $("#password").val();
		var repassword = $("#repassword").val();
		var identifyingCode = $("#identifying_code").val();

		if (!checkParams(email, password, repassword, identifyingCode)) {
			return;
		}

		// 注册
		var url = "/user/register/" + email + "/" + password + "/" + identifyingCode;
		syncGet(url, function(registSuccess) {
			if (registSuccess == 'true') {
				$(location).attr("href", "login.html");
			} else {
				alert("注册失败！");
			}
		});
	});

	// 点击发送验证码
	$("#get_identifying_link").click(function() {
		$("#get_identifying_link").attr('disabled', true).css("pointer-events", "none");

		var email = $("#email").val();
		var allowSend = true;
		if (email == null || email == '' || email == undefined) {
			alert("请输入邮箱！");
			allowSend = false;
		} else if (emailRegistered(email)) {
			alert("该邮箱已注册！");
			allowSend = false;
		}

		if (!allowSend) {
			$("#get_identifying_link").attr("disabled", false).css("pointer-events", "auto");
			return;
		}

		// 发送验证码
		var url = "/code/create/" + email;
		var time = 60;

		// 发送前先禁止点击, 倒计时60s
		offClickSometime("get_identifying_link", 60, "timeoutMonitor");
		// 禁用点击功能
		syncGet(url,
			function(createCode) {
				if (createCode == 'true') {
					alert("验证码已发送至邮箱，请注意查收！");
					$("#timeout").text();
				} else {
					alert("验证码发送失败，请重试！");
				}
			},
			function(result) {
				alert("验证码发送请求处理失败，请重试！");
			});
	});

	// 注册参数验证
	function checkParams(email, password, repassword, identifyingCode) {
		var pass = true;
		if (email == null || email == '') {
			alert("邮箱不能为空！");
			pass = false;
		} else if (emailRegistered(email)) {
			alert("该邮箱已注册！");
		} else if (password == null || password == '') {
			alert("请输入密码！");
			pass = false;
		} else if (repassword == null || repassword == '') {
			alert("请验证密码！");
			pass = false;
		} else if (password != repassword) {
			alert("两次输入密码不一致！");
			pass = false;
		} else if (identifyingCode == null || identifyingCode == '') {
			alert("请输入验证码！");
			pass = false;
		}

		return pass;
	}

	// 判断邮箱是否注册
	function emailRegistered(email) {
		var needReTry = false;
		var url = "/user/isRegistered/" + email;
		syncGet(url,
			function(registered) {
				if (registered == 'true') {
					needReTry = true;
				}
			},
			function(result) {
				needReTry = true;
			}
		);

		return needReTry;
	}

	/**
	 * 一段时间内禁止点击元素 
	 *
	 * @param {Object} disableId 禁止点击元素
	 * @param {Object} time 	 禁止时长
	 * @param {Object} monitorId 监控器id，用于显示倒计时
	 */
	function offClickSometime(disableId, time, monitorId) {
		$('#' + disableId).attr('disabled', true).css("pointer-events", "none");
		var monitor = $('#' + monitorId);
		var countDown = setInterval(function() {
			monitor.text("(" + time + ")");
			if (time <= 0) {
				// 计时结束，删除倒计时文本，停止计时，释放点击禁止
				monitor.text("");
				clearInterval(countDown);
				$('#' + disableId).attr("disabled", false).css("pointer-events", "auto");
			}
			time -= 1;
		}, 1000);
	}
});
